#include "filemanage.h"
#include<QFile>
#include<QFileDialog>
#include<QMessageBox>

QMutex FileManage::mutex;
FileManage* FileManage::instance=NULL;

QStringList FileManage::dirNames=QStringList()<<QDir::currentPath()+"/"+"Experiments/"
                                                <<QDir::currentPath()+"/"+"AnalyteDataBase/"
                                                  <<QDir::currentPath()+"/"+"Tune/"
                                                    <<QDir::currentPath()+"/"+"SysData/";

QStringList FileManage::fileSuffixs=QStringList()<<".exp"<<".ell"<<".tp";

QStringList FileManage::fileNames=QStringList()<<QDir::currentPath()+"/"+"SysData/"+"Tune.tp"
                                <<QDir::currentPath()+"/"+"SysData/"+"MassCalStd.cal"
                                <<QDir::currentPath()+"/"+"SysData/"+"MassCalHtd.cal"
                                <<QDir::currentPath()+"/"+"SysData/"+"CrossCalib.ccal"
                                <<QDir::currentPath()+"/"+"SysData/"+"Queue.qns"
                                <<QDir::currentPath()+"/"+"SysData/"+"TuneManualPara.sp"
                                <<QDir::currentPath()+"/"+"SysData/"+"AnalyteDataBase.ell"
                                <<QDir::currentPath()+"/"+"SysData/"+"WorkFlow.wf";

FileManage::FileManage()
{
    foreach(QString dirName,dirNames)
    {
        QDir dir(dirName);
        if(dir.exists()==false)
        {
            dir.mkpath(dirName);
        }
    }
}

//
QString FileManage::GetExpsDir()
{
    return  dirNames.at(0);
}
QString FileManage::GetDataBaseDir()
{
    return  dirNames.at(1);
}
QString FileManage::GetTuneManualDir()
{
   return  dirNames.at(2);
}
QString FileManage::GetSysDataDir()
{
   return  dirNames.at(3);
}

//
QString FileManage::GetExpsSuffix()
{
    return fileSuffixs.at(0);
}
QString FileManage::GetDataBaseSuffix()
{
    return fileSuffixs.at(1);
}
QString FileManage::GetTuneManualSuffix()
{
   return fileSuffixs.at(2);
}

//
QString FileManage::GetTuneFile()
{
    return fileNames.at(0);
}
QString FileManage::GetMassCalStdFile()
{
    return fileNames.at(1);
}
QString FileManage::GetMassCalHtdFile()
{
    return fileNames.at(2);
}
QString FileManage::GetCrossCalFile()
{
    return fileNames.at(3);
}
QString FileManage::GetQueueFile()
{
   return fileNames.at(4);
}
QString FileManage::GetTuneManualParaFile()
{
   return fileNames.at(5);
}
QString FileManage::GetAnalyteDataBaseFile()
{
   return fileNames.at(6);
}
QString FileManage::GetWorkFlowFile()
{
   return fileNames.at(7);
}
//
int FileManage::SaveToFile(QString fileName,Experiment *pExperiment)
{   
    QFile file(fileName);
    if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
        return -1;

    qint32 magic,qtVersion,version;
   // magic=SM->GetMagic();
    //qtVersion=SM->GetQtVersion();
    //version=SM->GetVersion();
    QDataStream out(&file);
    //out.setVersion(qtVersion);

    out<<magic;
    out<<qtVersion;
    out<<version;
    out << (*pExperiment);
    out<<magic;
    out<<qtVersion;
    out<<version;
    return 1;
}

int FileManage::LoadFromFile(QString fileName,Experiment *pExperiment)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))//ReadOnly ReadWrite
        return -1;
    QDataStream in(&file);
    //in.setVersion();
    qint32 magic,qtVersion,version;

    in>>magic;
    in>>qtVersion;
    in>>version;
    in >> (*pExperiment);
    in>>magic;
    in>>qtVersion;
    in>>version;

    return 1;
}


int FileManage::SaveToFile(QString fileName,AnalyteDataBase *pAnalyteDataBase)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
        return -1;

    qint32 magic,qtVersion,version;
   // magic=SM->GetMagic();
    //qtVersion=SM->GetQtVersion();
    //version=SM->GetVersion();
    QDataStream out(&file);
    //out.setVersion(qtVersion);

    out<<magic;
    out<<qtVersion;
    out<<version;
    out << (*pAnalyteDataBase);
    out<<magic;
    out<<qtVersion;
    out<<version;
    return 1;
}

int FileManage::LoadFromFile(QString fileName,AnalyteDataBase *pAnalyteDataBase)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
        return -1;
    QDataStream in(&file);
    //in.setVersion();
    qint32 magic,qtVersion,version;

    in>>magic;
    in>>qtVersion;
    in>>version;
    in >> (*pAnalyteDataBase);
    in>>magic;
    in>>qtVersion;
    in>>version;

    return 1;
}

int FileManage::SaveToFile(QString fileName,TuneResult *pTuneResult)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
        return -1;

    qint32 magic,qtVersion,version;
   // magic=SM->GetMagic();
    //qtVersion=SM->GetQtVersion();
    //version=SM->GetVersion();
    QDataStream out(&file);
    //out.setVersion(qtVersion);

    out<<magic;
    out<<qtVersion;
    out<<version;
    out << (*pTuneResult);
    out<<magic;
    out<<qtVersion;
    out<<version;
    return 1;
}
int FileManage::LoadFromFile(QString fileName,TuneResult *pTuneResult)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
        return -1;
    QDataStream in(&file);
    //in.setVersion();
    qint32 magic,qtVersion,version;

    in>>magic;
    in>>qtVersion;
    in>>version;
    in >> (*pTuneResult);
    in>>magic;
    in>>qtVersion;
    in>>version;

    return 1;
}

//
int FileManage::SaveToFile(QString fileName,CalRatio *pCalRatio)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
        return -1;

    qint32 magic,qtVersion,version;
   // magic=SM->GetMagic();
    //qtVersion=SM->GetQtVersion();
    //version=SM->GetVersion();
    QDataStream out(&file);
    //out.setVersion(qtVersion);

    out<<magic;
    out<<qtVersion;
    out<<version;
    out << (*pCalRatio);
    out<<magic;
    out<<qtVersion;
    out<<version;
    return 1;
}
int FileManage::LoadFromFile(QString fileName,CalRatio *pCalRatio)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
        return -1;
    QDataStream in(&file);
    //in.setVersion();
    qint32 magic,qtVersion,version;

    in>>magic;
    in>>qtVersion;
    in>>version;
    in >> (*pCalRatio);
    in>>magic;
    in>>qtVersion;
    in>>version;

    return 1;
}

int FileManage::SaveToFile(QString fileName,QStringList *queueExp)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
        return -1;

    qint32 magic,qtVersion,version;
   // magic=SM->GetMagic();
    //qtVersion=SM->GetQtVersion();
    //version=SM->GetVersion();
    QDataStream out(&file);
    //out.setVersion(qtVersion);

    out<<magic;
    out<<qtVersion;
    out<<version;
    out << (*queueExp);
    out<<magic;
    out<<qtVersion;
    out<<version;
    return 1;
}

int FileManage::LoadFromFile(QString fileName,QStringList *queueExp)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
        return -1;
    QDataStream in(&file);
    //in.setVersion();
    qint32 magic,qtVersion,version;

    in>>magic;
    in>>qtVersion;
    in>>version;
    in >> (*queueExp);
    in>>magic;
    in>>qtVersion;
    in>>version;

    return 1;
}

int FileManage::SaveToFile(QString fileName,TuneManual *pTuneManual)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
        return -1;

    qint32 magic,qtVersion,version;
   // magic=SM->GetMagic();
    //qtVersion=SM->GetQtVersion();
    //version=SM->GetVersion();
    QDataStream out(&file);
    //out.setVersion(qtVersion);

    out<<magic;
    out<<qtVersion;
    out<<version;
    out << (*pTuneManual);
    out<<magic;
    out<<qtVersion;
    out<<version;
    return 1;
}
int FileManage::LoadFromFile(QString fileName,TuneManual *pTuneManual)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
        return -1;
    QDataStream in(&file);
    //in.setVersion();
    qint32 magic,qtVersion,version;

    in>>magic;
    in>>qtVersion;
    in>>version;
    in >> (*pTuneManual);
    in>>magic;
    in>>qtVersion;
    in>>version;

    return 1;
}
int FileManage::SaveToFile(QString fileName,WorkFlow *workFlow)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadWrite|QIODevice::Truncate))
        return -1;

    qint32 magic,qtVersion,version;
   // magic=SM->GetMagic();
    //qtVersion=SM->GetQtVersion();
    //version=SM->GetVersion();
    QDataStream out(&file);
    //out.setVersion(qtVersion);

    out<<magic;
    out<<qtVersion;
    out<<version;
    out << (*workFlow);
    out<<magic;
    out<<qtVersion;
    out<<version;
    return 1;
}
int FileManage::LoadFromFile(QString fileName,WorkFlow *workFlow)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
        return -1;
    QDataStream in(&file);
    //in.setVersion();
    qint32 magic,qtVersion,version;

    in>>magic;
    in>>qtVersion;
    in>>version;
    in >> (*workFlow);
    in>>magic;
    in>>qtVersion;
    in>>version;

    return 1;
}
